var imkbizle = function() {

	//private area
	var httpRequest = null;
	var imkbizlePanel = null;
	var source = isnet; //TODO: allow user to select source site.

	var setLabel = function(stock) {
		imkbizlePanel.label = source.stockname + ": " + stock.Latest;
	};

	var setArrow = function(stock) {
		imkbizlePanel.image = "chrome://imkbizle/skin/arrow_"
			+ ((stock.Latest >= stock.Open) ? "up" : "down") + ".gif";
	};

	var setToolTipText = function(stock) {
		var tiptext = "";
		$H(stock).each(function(stat) {
			if (stat.value) {
				tiptext += source.locale.getString("stock." + stat.key) + ": " + stat.value + " | ";
			}
		});
		imkbizlePanel.tooltipText = tiptext.replace(/(.*) \| $/, "$1"); //strip last seperator
	};

	var infoReceived = function(transport) {
		var stock = source.getStock(transport.responseText);
		setLabel(stock);
		setArrow(stock);
		setToolTipText(stock);
	};

	var resetPanel = function() {
		imkbizlePanel.label = source.locale.getString("status.loading");
		imkbizlePanel.image = "";
		imkbizlePanel.tooltipText = source.locale.getString("status.wait");
	}

	// public area
	return {
		// Install a timeout handler to install the interval routine
		startup: function() {
			imkbizlePanel = document.getElementById('imkbizle');
			source.locale = document.getElementById("string-bundle"),
			source.stockname = imkbizlePanel.label;
			imkbizle.refreshInformation();
			window.setInterval(imkbizle.refreshInformation, 10*60*1000);
		},

		// Called periodically to refresh the stock information
		refreshInformation: function() {
			resetPanel();
			new Ajax.Request(source.url, {
				method: 'get',
				parameters: source.getParams(),
				onSuccess: infoReceived,
				onFailure: function(){ throw("couldn't retrieve data from '" + source.url + "'") }
			});
		},

		onSelectStock: function(event) {
			source.stockname = event.target.label;
			imkbizle.refreshInformation();
		}
	}
}()

// Install load handler
window.addEventListener("load", function(e) { imkbizle.startup(); }, false);
